<?php
require_once("../../config.inc.php");
require_once("../_inc/include.php");
$_SESSION[USEROBJ]->resetHistory();
$errMsg = "";
if (sxREQ('reqtype') == 'specupd') {
	$objUser->setHistoryVar(array('reqtype'=>''));
	specialUpdate();
}

//output HTML
$aOpz = array("tit2"=>_t("Welcome"));
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
//output menu' items
$aMenu = array(
					"1"=>array("pag"=>"../data/area/dataAreaList.php",
										"title"=>_t("Data Area, Entity, Relationship Management"),
										"text"=>_t("Data Area management"),
										"perm"=>"viewda"),
					"2"=>array("pag"=>"../data/domn/domainList.php",
										"title"=>_t("Domains management"),
										"text"=>_t("Domains management"),
										"perm"=>"viewdom"),
					"3"=>array("pag"=>"../proc/modl/moduleList.php",
										"title"=>_t("Process-modules management, Rules, Layouts"),
										"text"=>_t("Process-modules management"),
										"perm"=>"viewmod"),
					"4"=>array("pag"=>"../proc/libr/libraryList.php",
										"title"=>_t("Library Functions management"),
										"text"=>_t("Library Functions management"),
										"perm"=>"viewfun"),
					"8"=>array("pag"=>"../uspr/user/userList.php",
										"title"=>_t("Users management"),
										"text"=>_t("Users management"),
										"perm"=>"viewuser"),
					"9"=>array("pag"=>"../uspr/prof/profileList.php",
										"title"=>_t("Prolfile management"),
										"text"=>_t("Prolfile management"),
										"perm"=>"viewpro"),
					"16"=>array("pag"=>"../data/sync/syncDB.php",
										"title"=>_t("Data-base sinchronization"),
										"text"=>_t("Data-base sinchronization"),
										"perm"=>"syncdb"),
					"17"=>array("pag"=>"../proc/sync/syncCode.php",
										"title"=>_t("Rule/Function code generation"),
										"text"=>_t("Rule/Function code generation"),
										"perm"=>"syncdb"),
					"20"=>array("pag"=>"../util/imex/directUploadMB.php",
										"title"=>_t("Direct compare/import of all repository"),
										"text"=>_t("Direct compare/import"),
										"perm"=>"syncdb"),
					"21"=>array("pag"=>"../util/imex/uploadMB.php",
										"title"=>_t("Compare/import from file"),
										"text"=>_t("Compare/import from file"),
										"perm"=>"syncdb"),
					"25"=>array("pag"=>"../util/imex/genReleaseMB.php",
										"title"=>_t("Release generation"),
										"text"=>_t("Release generation"),
										"perm"=>"syncdb"),
					"27"=>array("pag"=>"../util/integrityCheck.php",
										"title"=>_t("Referential integrity check"),
										"text"=>_t("Referential integrity check"),
										"perm"=>"syncdb"),
					"28"=>array("pag"=>"menu.php?reqtype=specupd",
										"title"=>_t("Special updates"),
										"text"=>_t("Special updates"),
										"perm"=>"syncdb")
					);
echo(menuHTML($aMenu));	
//data output
if($errMsg != ""){
	
}
echo(footerHTML());

function menuHTML($aMenu){
	//commands
	$objUser = $_SESSION[USEROBJ];
	$numMenu = 0;
	$sHTML = "<table width='100%'align='center' cellspacing='0' cellpadding='6' border='0'>\n";
	$sHTML .= "<tr><td class=\"menu01\" style='text-align:center;' colspan='2'><i>".
		_t("Menu")."</i></td></tr>\n";
	for($k=1; $k<15; $k++){	
		$j=$k+15;
		$aLM = (isset($aMenu[$k]))?$aMenu[$k]:array();
		$aRM = (isset($aMenu[$j]))?$aMenu[$j]:array();
		$bHavePerm = false;
		if(isset($aLM["text"])){
			$cmdPerm = (isset($aLM["perm"]))?$aLM["perm"]:"";
			//check data area permission 
			if($cmdPerm == ""){
				$bHavePerm = true;
			}
			//check user permission 
			if($objUser->getPermission($cmdPerm) == "Y"){
				$bHavePerm = true;
			}
		}
		if($bHavePerm){	
			if(isset($aLM["pag"])){
				$zz = (strpos($aLM["pag"],'?')===false)?'?':'&';
				$sHTML .= "<tr><td class=\"menu02\" width='50%'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				$sHTML .= "<a class=\"menu02\" href=\"javascript:pageRedir('".$aLM["pag"].$zz."xsn=".XSN."');\" ";
				if(isset($aLM["title"]))
					$sHTML .= "alt=\"".$aLM["title"]."\" title=\"".$aLM["title"]."\"";
				$sHTML .= ">$k. ".$aLM["text"]."</a>";
				$sHTML .= "</td>\n";
			}
		} else {
				$sHTML .= "<td class=\"menu02\" width='50%'>";
				$sHTML .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				$sHTML .= "</td>";
		}
		$bHavePerm = false;
		if(isset($aRM["text"])){
			$cmdPerm = (isset($aRM["perm"]))?$aRM["perm"]:"";
			//verifico le permission sulla data area
			if($cmdPerm == ""){
				$bHavePerm = true;
			}
			//verifico le permission dell'utente'
			if($objUser->getPermission($cmdPerm) == "Y"){
				$bHavePerm = true;
			}
		}
		if($bHavePerm){	
			if(isset($aRM["pag"])){
				$zz = (strpos($aRM["pag"],'?')===false)?'?':'&';
				$sHTML .= "<td class=\"menu02\" width='50%'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				$sHTML .= "<a class=\"menu02\" href=\"".$aRM["pag"].$zz."xsn=".XSN."\" ";
				if(isset($aRM["title"]))
					$sHTML .= "alt=\"".$aRM["title"]."\" title=\"".$aRM["title"]."\"";
				$sHTML .= ">$j. ".$aRM["text"]."</a>";
				$sHTML .= "</td></tr>\n";
			}
		} else {
				$sHTML .= "<td class=\"menu02\" width='50%'>";
				$sHTML .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
				$sHTML .= "</td></tr>\n";
		}
	}
	$sHTML .= "</table>\n";
	return $sHTML;
}

function specialUpdate() { 
	dbg_("*** SPECIAL UPDATE ***");
	setRowSeqTableLine();
	checkAliasWXY();
}

function setRowSeqTableLine() { 
	global $objSDAO;
	dbg_("*** Imposta a 1 band e row per layout_set type='table-line' ***");
	$sSQL = "SELECT DISTINCT lf.layout_set FROM layout_field lf " .
			"INNER JOIN layout_set ls ON ls.layout_set = lf.layout_set  " .
			"WHERE ls.type = 'table-line' AND (lf.band <> 1 or lf.row <> 1)";
	$objSDAO->query($sSQL,'ls');
	while($row = $objSDAO->fetch('ls')) {
		dbg_("layout_set:".$row['layout_set']);
		$sSQL = "DELETE FROM layout_band WHERE layout_set = '".$row['layout_set']."'";
		$objSDAO->exec($sSQL);
		$sSQL = "UPDATE layout_field SET seq = seq + 90000, band = band + 90001 " .
				" WHERE layout_set = '".$row['layout_set']."'";
		$objSDAO->exec($sSQL);
		$sSQL = "SELECT layout_set, layout, band, row, seq FROM layout_field " .
				" WHERE layout_set = '".$row['layout_set']."' AND band > 90000" .
				" ORDER BY layout, band, row, seq ";
		$objSDAO->query($sSQL,'lf');
		$layout = -1;
		while($rowF = $objSDAO->fetch('lf')) {
			if ($layout != $rowF['layout']) {
				$layout = $rowF['layout'];
				$nr = 0;
				dbg_("layout:".$rowF['layout']);
				$sSQL = "INSERT INTO layout_band (layout_set, layout, band, flags, display_only, description, note) " .
						" VALUES('".$rowF['layout_set']."', ".$rowF['layout'].", 1, '', 'N', 'no-band','')";	
				$objSDAO->exec($sSQL);
			}
			dbg_("layout_field:".$rowF['layout'].'-'.$rowF['band'].'-'.$rowF['row'].'-'.$rowF['seq']);
			$nr += 10;
			$sSQL = "UPDATE layout_field SET seq = $nr, row = 1, band = 1 " .
					" WHERE layout_set = '".$rowF['layout_set']."'".
					"    AND layout = ".$rowF['layout'].
					"    AND band = ".$rowF['band'].
					"    AND row = ".$rowF['row'].
					"    AND seq = ".$rowF['seq'];
			$objSDAO->exec($sSQL);
		}
	}
}

function checkAliasWXY() {
	global $objSDAO;
	dbg_("*** check existence aliases W,X e Y for all processes ***");
	$sSQL = "SELECT al.alias, pu.process_unit FROM process_unit pu LEFT JOIN pu_alias al ON al.process_unit = pu.process_unit";
	$objSDAO->query($sSQL,'verali');
	$aPuWXY = array();
	while($rowWXY=$objSDAO->fetch('verali')) { $aPuWXY[$rowWXY['process_unit']][] = $rowWXY['alias']; }
	foreach($aPuWXY as $process_unit => $aWXY) {
		if (!in_array('W',$aWXY)) {
			$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
					" VALUES('".sxAS($process_unit)."','W','','memory variables','','N')";
			$objSDAO->exec($sSQL);
		}
		if (!in_array('X',$aWXY)) {
			$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
					" VALUES('".sxAS($process_unit)."','X','','1 dimension array','','N')";
			$objSDAO->exec($sSQL);
		}
		if (!in_array('Y',$aWXY)) {
			$sSQL = "INSERT INTO pu_alias (process_unit,alias,entity,description,note,read_only) " .
					" VALUES('".sxAS($process_unit)."','Y','','2 dimensions array','','N')";
			$objSDAO->exec($sSQL);
		}	
	}
	//check existence aliases W,X e Y for all layout_set
	$sSQL = "SELECT al.layout_alias, ls.layout_set FROM layout_set ls " .
			" LEFT JOIN layout_alias al ON al.layout_set = ls.layout_set";
	$objSDAO->query($sSQL,'verali');
	$aLsWX = array();
	while($rowWX=$objSDAO->fetch('verali')) { $aLsWX[$rowWX['layout_set']][] = $rowWX['layout_alias']; }
	foreach($aLsWX as $layout_set => $aWX) {
		if (!in_array('W',$aWX)) {
			$sSQL = "INSERT INTO layout_alias (layout_set,layout_alias,entity) " .
					" VALUES('".sxAS($layout_set)."','W','')";
			$objSDAO->exec($sSQL);
		}
		if (!in_array('X',$aWX)) {
			$sSQL = "INSERT INTO layout_alias (layout_set,layout_alias,entity) " .
					" VALUES('".sxAS($layout_set)."','X','')";
			$objSDAO->exec($sSQL);
		}
	}
	//check association all aliases w, e Y for all frames
	$sSQL = "SELECT f.process_unit, f.frame, f.layout_set, fa.layout_alias FROM frame f " .
			" LEFT JOIN frame_alias_match fa ON fa.process_unit = f.process_unit " .
			"    AND fa.frame = f.frame";
	$objSDAO->query($sSQL,'verali');
	$aFaWX = array();
	while($rowWX=$objSDAO->fetch('verali')) { 
		$aFaWX[$rowWX['process_unit']][$rowWX['frame']][$rowWX['layout_set']][] = $rowWX['layout_alias']; 
	}
	foreach($aFaWX as $process_unit => $a1) {
		foreach($a1 as $frame => $a2) {
			foreach($a2 as $layout_set => $aWX) {
				if (!in_array('W',$aWX)) {
					$sSQL = "INSERT INTO frame_alias_match (process_unit,frame,layout_set,layout_alias,alias) " .
							" VALUES('".sxAS($process_unit)."','".sxAS($frame)."','".sxAS($layout_set)."','W','W')";
					$objSDAO->exec($sSQL);
				}
				if (!in_array('X',$aWX)) {
					$sSQL = "INSERT INTO frame_alias_match (process_unit,frame,layout_set,layout_alias,alias) " .
							" VALUES('".sxAS($process_unit)."','".sxAS($frame)."','".sxAS($layout_set)."','X','X')";
					$objSDAO->exec($sSQL);
				}
			}
		}
	}
}

?>